using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.Tilemaps;
using UnityEngine.UI;

// 导出地图为txt文件
// 第一行两个数 x轴长度 y轴长度
// 接下来y行数据，每行x个数，'0'表示可行区域，'1'表示障碍物或边境

public class OutputMap : MonoBehaviour
{
    public Button btn;
    public Tilemap map;
    private string filepath = @"C:\Users\caomb\Desktop\实训\地图.txt";
    // private string filepath = @"C:\Users\19361\Desktop\高级实训\地图.txt";
    // Start is called before the first frame update
    void Start()
    {
        btn.onClick.AddListener(foo);
    }

    // Update is called once per frame
    void foo(){
        Debug.Log("onclick: output the map");

        int x = map.cellBounds.x;
        int y = map.cellBounds.y;
        int sizeX = map.cellBounds.size.x;
        int sizeY = map.cellBounds.size.y;

        StreamWriter sw = new StreamWriter(filepath, false);
        sw.WriteLine(sizeX + " " + sizeY);
        for(int j = 0;j < sizeY;j ++){
            // string tmp = new String('1', sizeX);
            char[] tmp = new char[sizeX];
            for(int i = 0;i < sizeX;i ++){
                if(map.HasTile(new Vector3Int(x+i, y+j,0))){
                    tmp[i] = '0';
                }
                else{
                    tmp[i] = '1';
                }
            }
            sw.WriteLine(tmp);
        }
        sw.Close();
    }
}
